package com.telecom.util;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.util.Map;
@AllArgsConstructor
@NoArgsConstructor
public class ProxyImpl implements InvocationHandler {
    private Map<String,Mapper> mappers;
    private Connection connection;
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String methodName = method.getName();//selectAll
        String className = method.getDeclaringClass().getName();//获取到类名
        //com.telecom.mapper.StudentMapper
        // com.telecom.mapper.StudentMapper.selectAll
        Mapper mapper = mappers.get(className+"."+methodName);
        if (mapper==null){
            throw new RuntimeException("没有找到对应的SQL语句");
        }
        return new DBUtil().selectList(connection, mapper);
    }
}
